package com.itheima.pay.controller;

import com.alipay.api.AlipayApiException;

import com.alipay.easysdk.factory.Factory;
import com.alipay.easysdk.kernel.Config;
import com.alipay.easysdk.kernel.util.ResponseChecker;
import com.alipay.easysdk.payment.common.models.AlipayTradeFastpayRefundQueryResponse;
import com.alipay.easysdk.payment.common.models.AlipayTradeQueryResponse;
import com.alipay.easysdk.payment.common.models.AlipayTradeRefundResponse;
import com.alipay.easysdk.payment.facetoface.models.AlipayTradePrecreateResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * @Description:
 * @Version: V1.0
 */
@RestController
@RequestMapping("easyPay")
public class EasyPayController {

  @Autowired
  Config config;

  // 生成交易二维码
  @GetMapping("precreate/{orderNo}")
  public AlipayTradePrecreateResponse precerate(@PathVariable String orderNo) throws AlipayApiException {
    // 1. 设置参数（全局只需设置一次）
    Factory.setOptions(config);
    try {
      // 2. 发起API调用（以创建当面付收款二维码为例）
      AlipayTradePrecreateResponse response = Factory.Payment.FaceToFace().preCreate("华为P30", orderNo, "0.01");
      // 3. 处理响应或异常
      if (ResponseChecker.success(response)) {
        System.out.println("调用成功");
        System.out.println(response.getQrCode());
        return response;
      } else {
        System.err.println("调用失败，原因：" + response.msg + "，" + response.subMsg);
      }
    } catch (Exception e) {
      System.err.println("调用遭遇异常，原因：" + e.getMessage());
      throw new RuntimeException(e.getMessage(), e);
    }

      return null;
  }

  @GetMapping("query/{orderNo}")
  public AlipayTradeQueryResponse query(@PathVariable String orderNo) throws Exception {
    // 1. 设置参数（全局只需设置一次）
    Factory.setOptions(config);
    try {
      // 2. 发起API调用（以创建当面付收款二维码为例）
      AlipayTradeQueryResponse response = Factory.Payment.Common().query(orderNo);
      // 3. 处理响应或异常
      if (ResponseChecker.success(response)) {
        System.out.println("调用成功");
        System.out.println(response.getTradeStatus());
        return response;
      } else {
        System.err.println("调用失败，原因：" + response.msg + "，" + response.subMsg);
      }
    } catch (Exception e) {
      System.err.println("调用遭遇异常，原因：" + e.getMessage());
      throw new RuntimeException(e.getMessage(), e);
    }

    return null;
  }

  @GetMapping("refund/{orderNo}/{amount}")
  public AlipayTradeRefundResponse refund(@PathVariable String orderNo, @PathVariable String amount) throws Exception {
    // 1. 设置参数（全局只需设置一次）
    Factory.setOptions(config);
    try {
      // 2. 发起API调用（以创建当面付收款二维码为例）
      AlipayTradeRefundResponse response = Factory.Payment.Common().refund(orderNo, amount);
      // 3. 处理响应或异常
      if (ResponseChecker.success(response)) {
        System.out.println("调用成功");
        System.out.println(response.getRefundFee());
        return response;
      } else {
        System.err.println("调用失败，原因：" + response.msg + "，" + response.subMsg);
      }
    } catch (Exception e) {
      System.err.println("调用遭遇异常，原因：" + e.getMessage());
      throw new RuntimeException(e.getMessage(), e);
    }

    return null;
  }
    @GetMapping("fastpay/{orderNo}/{no}")
    public  AlipayTradeFastpayRefundQueryResponse fastpay(@PathVariable String orderNo,@PathVariable String no) throws Exception {
// 1. 设置参数（全局只需设置一次）
      Factory.setOptions(config);
      try {
        // 2. 发起API调用（以创建当面付收款二维码为例）
        AlipayTradeFastpayRefundQueryResponse response = Factory.Payment.Common().queryRefund(orderNo, no);
        // 3. 处理响应或异常
        if (ResponseChecker.success(response)) {
          System.out.println("调用成功");
          return response;
        } else {
          System.err.println("调用失败，原因：" + response.msg + "，" + response.subMsg);
        }
      } catch (Exception e) {
        System.err.println("调用遭遇异常，原因：" + e.getMessage());
        throw new RuntimeException(e.getMessage(), e);
      }

      return null;
  }

}